/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs;

import daos.ItemDAO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Item;
import models.ShoppingCart;
import util.Common;

/**
 *
 * @author trietto
 */
public class OrderForm extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
            String[] paymentData = new String[]{"By cheque\tBy cheque", "By cash\tBy cash"};
            String[] shipmentData = new String[]{"3 days shipping\t3 days shipping", "1 week shipping\t1 week shepping"};
            writer.println("<form action='OrderServlet' method='POST'>");
            writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            writer.println("<tr>");
            writer.println("<td colspan='2' align='center'><b>ORDER BOOK</b><br></td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Order Date:</td>");
            writer.println("<td>" + sdf.format(new Date()) + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Payment:</td>");
            writer.println("<td>" + Common.createComboBox("cbPayment", "cbPayment", paymentData, "", "") + "</td>");
            writer.println("</tr>");
            writer.println("<tr>");
            writer.println("<td>Shipping:</td>");
            writer.println("<td>" + Common.createComboBox("cbShipping", "cbShipping", shipmentData, "", "") + "</td>");
            writer.println("</tr>");
            writer.println("<tr colspan='2'>");
            writer.println("<td>" + Common.createSubmitButton("btnBuy", "btnBuy", "Buy", "") + "</td>");
            writer.println("</tr>");
            writer.println("</table>");

            ArrayList<ShoppingCart> lstCarts = (ArrayList<ShoppingCart>) (pageContext.getSession().getAttribute("carts"));
            if (lstCarts == null) {
                lstCarts = new ArrayList<ShoppingCart>();
            }

            writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            Item item = null;
            for (ShoppingCart shoppingCart : lstCarts) {
                item = ItemDAO.singleton().select(shoppingCart.getItemId());
                writer.println("<tr>");
                writer.println("<td>Book:</td>");
                writer.println("<td>" + item.getName() + "</td>");
                writer.println("<td>Quantity:</td>");
                writer.println("<td>" + shoppingCart.getQuantity() + "</td>");
                writer.println("<td>Price:</td>");
                writer.println("<td>" + shoppingCart.getPrice() + "</td>");
                writer.println("</tr>");
            }          
            writer.println("</table>");

            writer.println("</form>");
        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
